package com.wanmait.movie.controller;

import com.wanmait.movie.entity.Admin;
import com.wanmait.movie.entity.Movie;
import com.wanmait.movie.entity.VO.MovieDTO;
import com.wanmait.movie.service.ActorService;
import com.wanmait.movie.service.MovieService;
import com.wanmait.movie.service.MovieTypeService;
import com.wanmait.movie.service.MovieTypesService;
import com.wanmait.movie.util.AjaxResult;
import com.wanmait.movie.util.UploadFileUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("manage/movie")
public class ManageMovieController {
    @Resource
    private MovieService movieService;
    @Resource
    private MovieTypeService movieTypeService;
    @Resource
    private MovieTypesService movieTypesService;
    @Resource
    private UploadFileUtil uploadFileUtil;
    @Resource
    private ActorService actorService;

    @GetMapping("list")
    public AjaxResult list(Integer pageNum, Integer movieTypeId, String keyword){
        if (pageNum==null||pageNum==0){
            pageNum=1;
        }
        if (movieTypeId==null){
            movieTypeId=0;
        }
        if (keyword==null){
            keyword="";
        }
        return AjaxResult.success(movieService.getESMovieList(pageNum, movieTypeId, keyword)).put("movieType",movieTypeService.getMovieTypeList("")).put("total",movieService.getESMovieList(pageNum, movieTypeId, keyword).getTotal());
    }

    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        movieService.removeById(id);
        return AjaxResult.success("删除成功");
    }

    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody MovieDTO movieDTO, HttpServletRequest request){
        Admin admin = (Admin) request.getAttribute("admin");
        boolean code=movieService.saveOrUpdate(movieDTO,admin);
        if (code) {
            return AjaxResult.success("添加或更新成功");
        } else {
            return AjaxResult.error("数据有误，添加或更新失败");
        }
    }

    @GetMapping("findMovieById")
    public AjaxResult findMovieById(Integer id){
        Movie movie = movieService.getById(id);
        List<Integer> movieTypeList = movieTypesService.getMovieTypeListByMovieId(id);
        MovieDTO movieDTO = new MovieDTO();
        movieDTO.setId(id).setMovieName(movie.getMovieName()).setMoviePic(movie.getMoviePic())
                .setActorDirector(movie.getActorDirector()).setIntroduction(movie.getIntroduction())
                .setLanguage(movie.getLanguage()).setMovieTypeList(movieTypeList).setEndTime(movie.getEndTime())
                .setReleaseTime(movie.getReleaseTime()).setSource(movie.getSource()).setTime(movie.getTime());
        return AjaxResult.success(movieDTO);
    }

    @GetMapping("getMovieTime")
    public AjaxResult getMovieTime(Integer id){
        return AjaxResult.success(movieService.getMovieTime(id));
    }
}
